import Vue from 'vue'
import Vuex from 'vuex'
import { getDetailedList, getCompanyInfo } from '@/common/api.js'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    dictList: null, //字典表
    // companyInfo: JSON.parse(localStorage.getItem('companyInfo')), // 企业信息
    companyInfo: null, // 企业信息
  },
  mutations: {
    // 保存字典表
    setDictList(state,data) {
      // 把值保存到dictList中
      state.dictList = {...data}
    },
    setCompanyInfo(state, data) {
      localStorage.setItem('companyInfo', JSON.stringify(data))
      state.companyInfo = data
    },
    // 设置企业信息
    reFreshCompanyInfo(state) {
      state.companyInfo = JSON.parse(localStorage.getItem('companyInfo'))
    }
  },
  actions: {
    // 获取字典表
    async getDictList({ commit }) {
      let res = await getDetailedList(`all`)
      commit("setDictList",res.data)
      console.log("全部字典表",res)
    },
    async reGetCompanyInfo({ commit }) {
      let data = {
        clueSource: JSON.parse(localStorage.getItem('login')).clueSource,
      }
      let res = await getCompanyInfo(data)
      if(res.code == 200) {
        commit('setCompanyInfo', res.data)
      }
    }
  },
  modules: {
  }
})
